package cn.tedu.boot42.controller;

import cn.tedu.boot42.entity.User;
import cn.tedu.boot42.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/check")
    public String check(String username){
        User user=mapper.selectByUsername(username);
        if (user!=null){
            return "用户名已存在";
        }
        return "用户名可用!";
    }


    @RequestMapping("/reg")
    public int reg(User user){
        //查询用户是否存在
        User u=mapper.selectByUsername(user.getUsername());
        if (u!=null){
            return 0;//用户名已存在
        }
        //把用户信息保存到数据库中
        mapper.insert(user);
        return 1;
    }

    @RequestMapping("/select")
    public List<User> select(){
        return mapper.select();
    }

}
